/**
 * 注意，该工具类函数文件需要statusCode.js文件
 */



/**
 * 该函数用于获取Url里的参数
 * @param varName 需要获取的参数名称
 * @returns String 返回参数的值
 */
function getQueryVariable(varName) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i= 0; i<vars.length; i++) {
        var pair = vars[i].split("=");
        if(pair[0] == varName){
            return pair[1];
        }
    }
    return(false);
}

/**
 * 该函数用于替换Url里的参数，该函数并不会直接修改链接
 * @param url 目标url
 * @param arg 需要替换的参数名称
 * @param arg_val 替换后的参数的值
 * @returns String 返回参数替换后的url
 */
function getChangeURLArg(url,arg,arg_val){
    var pattern=arg+'=([^&]*)';
    var replaceText=arg+'='+arg_val;
    if(url.match(pattern)){
        var tmp='/('+ arg+'=)([^&]*)/gi';
        tmp=url.replace(eval(tmp),replaceText);
        return tmp;
    }else{
        if(url.match('[\?]')){
            return url+'&'+replaceText;
        }else{
            return url+'?'+replaceText;
        }
    }
    return url+'\n'+arg+'\n'+arg_val;
}


/**
 * 该函数用于判断当前信息码的类型，并给出相应的信息提示
 */
function alertByStatusCode() {
    var statusCode = parseInt(getQueryVariable("code"));

    var success = "success";//成功
    var info = "info";//消息
    var warning = "warning";//警告
    var danger = "danger";//危险

    var msgStr = null;
    var msgType = null;

    switch (statusCode){
        case LOGIN_EXPIRED:
            msgStr = "<strong>警告！</strong>你的登录已过期！请重新登录";
            msgType = danger;
            break;
        case UNAUTHORIZED:
            msgStr = "<strong>警告！</strong>你没有权限访问该页面！请登录！";
            msgType = danger;
            break;
        case TOKEN_NOT_EXIST:
            msgStr = "<strong>警告！</strong>该用户token不存在！";
            msgType = warning;
            break;
        case BACKEND_NO_RESPOND:
            msgStr = "<strong>警告！</strong>服务器后端未响应！";
            msgType = danger;
            break;
        case LOGIN_SUCCESS:
            msgStr = "登录成功！";
            msgType = success;
            break;
        case REGISTER_SUCCESS:
            msgStr = "注册成功！请返回登录";
            msgType = success;
            break;
        case LOGOUT_SUCCESS:
            msgStr = "注销成功！";
            msgType = success;
            break;
        default:
            break;
    }
    //消息不为空时才能提醒
    if ( msgStr !== null){
        alertUtil.message(msgStr,msgType);
        console.log("有执行alertByStatusCode");
    }

}
